Lesson 2: Working with Functions
with Minecraft Pi Edition API
Aims:
-
Learn about the important libraries and functions.
-
Gain an understanding of how to use functions and libraries.
-
Learn how to install 3rd party libraries and use the functions.
-
Become more familiar with python syntax especially in functions.
Important Libraries to
include
A
function takes in input and does certain operations then gives the output.
However before we get into detail about what functions to use for Minecraft Pi Edition,we
should understand that there are certain libraries we need to include in our
program.
A
library contains the definition of the functions, such as what it does or how
the function behaves if it is given an input. By including the library at the start
of our program, we are able to use the functions that are included in the
particular library.
For
example to import the standard library for Minecraft it would be:
import block
import minecraft
Above
shows the general syntax for adding libraries to your text editor. This line of
code should generally be included at the start of every program you make. To
explain, import takes the functions of the library block.py so that you can use
block functions, for the sole purpose of adding/removing or modifying blocks in
general.
Also
another important library to add is minecraft.py this allows users to use functions
that connect and interact with the game. For example one is able to change the
angle of the camera, change positions of players, settings and so on. However
do be aware that there are many more libraries to use, leaving room for plenty
of creativity.
Important Functions to use
These two
libraries are the most important and you will always see these two libraries.
However
the next few functions worth mentioning as they do crop up a lot are these:
mc = minecraft.Minecraft.create()
This allows the user to connect to minecraft. This
creates the minecraft object and assigns it to a variable. Our minecraft object
needs to be always running in the game, so including this line of code is
important.
Pos = mc.player.getTilePos()
The next function gets the players position, this is
important if you decide to use an animated character to place your blocks down,
or if you generally just like to roam around the maps available.
Composition of different types of Functions and Libraries
Following up from this, there are some functions you
can type in that allow you to control the movement of your character, however
this does require that you import the minecraftturtle library:
(Do note that you must install the library
minecraftturtle to use the functions the instructions are provided in this link
: http://www.stuffaboutcode.com/2014/05/minecraft-graphics-turtle.html)
This demonstrates that 3rd parties are able
to create their own libraries and functions and that other people can install
and use them to further increase their creative powers.
Import minecraftturtle
playerPos = minecraftturtle.MinecraftTurtle(mc, pos)
Nameofyourvariable.Direction(number of blocks you want
to move)
playerPos.right(180) Ð Moves the player to the right
180 degrees.
playerPos.up(30) Ð Moves the player 40 degrees
upwards.
playerPos.forward(45) Ð Moves the player 45 blocks
forward
This code will run from the top to bottom. This is to
emphasize the effectiveness of combining multiple functions to get a different
output. If we combine all these functions together, we should have a line 45
blocks long at a 30-degree angle upward from behind. Do note that this code
alone will not create what is needed. You will need to import the important
libraries as stated above.
Instead of making a line however, we may want to add
instead just a single block at a specific location.
mc.setBlock(x,y,z,type)
This line of code will set a block of a specific type
and location x.y,z. By having this function we are able to gain more precision
in our plotting of blocks. There is an extensive list of block types that one
could use, the link to this will be provided at the end of the tutorial.
As an example of how this block of code works:
mc.setBlock(0,1,2,block.WOOL.id)
This line of code will set a block of grass at
position 0 blocks along the x-axis, 1 block along the y-axis and 2 blocks along
the z-axis.
You may however prefer a different colour of grass
instead of the default colour, so by adding a sub-type from as shown below. The
numbers generally have a range depending on their type.
mc.setBlock(0,1,2,block.WOOL.id.12)
Now my block of wool is brown, this will allow me to create different types of
sheep. Do note however only a specific number of types are able to change
colour, so do please refer to the link provided to see what types can change.
We must also include the line:
position = vec3.vec(0,0,0)
Where the number 0 is interchangeable with other
numbers. Vec is short for vector, so the function means there includes 3
vectors in Minecraft, position.x, position.y, and position.z.
These are used to represent a specific location in
Minecraft, whereby x and z are the horizontal positions of the game and y is
the vertical.
We can also store the position of each position into a
variable for ease of reference such as:
Posofx = position.x
Posofy = position.y
Posofz = position.z
There is
however an extensive list of functions/libraries that you can use this is on
the website do check it out! http://www.stuffaboutcode.com/p/minecraft-api-reference.html
Exercises
In these
exercises make sure to import the correct libraries and use the correct
functions.
Must Do Questions
1) Draw a line 30 blocks long
(4 marks)
2) Draw a line 40 blocks long
in a 30 degree upward angle
(4 marks)
3) Create a block where x = 1,
y = 2, z = 4 that must be of type WOOL and with id 10.
(4 marks)
4) Create a block where x =2 ,
y = 2 , z = 2 that must be of type SANDSTONE and id 2.
(4 marks)
Total: /20
Challenge Question
Applying the knowledge of
the notes and the link provided, create a school related item using blocks of
your choice.